/**
 * projectName: mbzwb-parent
 * fileName: GlobalExceptionHandler.java
 * packageName: com.palmdoctor.mb.base.exception
 * date: 2021-07-20 15:56
 * copyright(c) 2021 http://www.hydee.cn/ Inc. All rights reserved.
 */
package com.zny.sescore.exception;

import com.zny.sescore.model.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @version: V1.0
 * @author: zhangnayi
 * @className: GlobalExceptionHandler
 * @packageName: com.palmdoctor.mb.base.exception
 * @description:
 * @data: 2021-07-20 15:56
 **/
@Slf4j
@ResponseBody
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)  //处理所有的异常
    public ApiResult<String> exceptionHandle(HttpServletRequest request, Exception ex) {
        //将异常进行分类，不同的异常进行不同的处理
        ApiResult apiResult = null;
        log.error("", ex);
        if (ex instanceof BizException) {
            BizException be = (BizException) ex;
            apiResult = ApiResult.failed(be.getCode(), be.getMessage());
        } else {
            apiResult = ApiResult.failed(MessageEnum.BIZ_ERROR);
        }
        return apiResult;
    }
}